package mypackage;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.SocketConnection;
import javax.microedition.media.Control;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.protocol.ContentDescriptor;
import javax.microedition.media.protocol.DataSource;
import javax.microedition.media.protocol.SourceStream;
import net.rim.device.api.media.control.AudioPathControl;
import net.rim.device.api.media.protocol.ByteArrayInputStreamDataSource;

public class PlayThread extends Thread implements PlayerListener
{
    private byte[] playByte;

    private ByteArrayInputStream stream;

    private Player player;

    private long startTime = 0;

    private int readLength = 0;

    private String contentType = "audio/amr";
    
    /*
    private SourceStream mInput = new SourceStream()
    {
        long mDebugSeq = 0;

        private int mAvailableSize = 0;

        private int mPacketSize;

        private ContentDescriptor mContentDescriptor = new ContentDescriptor(
            contentType);

        public int read(byte[] b, int offset, int length) throws IOException
        {
            Log.d("read", "read: to b , lenght:" + length + " offset:" + offset
                + " time: " + System.currentTimeMillis());
            

            Log.d("read", "b.length:" + b.length + " offset:" + offset
                + " length:" + length);

            if (startTime == 0)
            {
                startTime = System.currentTimeMillis();
            }

            if ((readLength + length) < playByte.length)
            {
                System.arraycopy(playByte, readLength, b, offset, length);
                readLength += length;

                Log.d("read", "total:" + readLength);

                return length;
            }
            else
            {
                Log.d("read", "read over");
                return 1;
            }

        }

        public ContentDescriptor getContentDescriptor()
        {
            Log.d("read", "getContentDescriptor");
            return mContentDescriptor;
        }

        public long getContentLength()
        {
            Log.d("read", "getContentLength");
            return -1;
        }

        public int getSeekType()
        {
            Log.d("read", "getSeekType");
            return SourceStream.SEEKABLE_TO_START;
        }

        public int getTransferSize()
        {
            Log.d("read", "SourceStream getTransferSize()");
            return 16*1024;
        }

        public long seek(long where) throws IOException
        {
            Log.d("read", "seek to : " + where);
            return 0;
        }

        public long tell()
        {
            Log.d("read", "tell");
//            return startTime == 0 ? 0
//                : (System.currentTimeMillis() - startTime);
            return readLength;
        }

        public Control getControl(String controlType)
        {
            Log.d("read", "getControl");
            return null;
        }

        public Control[] getControls()
        {
            Log.d("read", "getControls");
            return null;
        }
    };
    */

    
    private InputStream mInput = new InputStream()
    {
        public int read(byte[] b, int offset, int length) throws IOException
        {
            Log.d("read", "read: to b , lenght:" + length + " offset:" + offset
                + " time: " + System.currentTimeMillis());
            Log.d("read", "b.length:" + b.length + " offset:" + offset
                + " length:" + length);
            if (startTime == 0)
            {
                startTime = System.currentTimeMillis();
            }
            if ((readLength + length) < playByte.length)
            {
                System.arraycopy(playByte, readLength, b, offset, length);
                readLength += length;
                Log.d("read", "total:" + readLength);
                return length;
            }
            else
            {
                Log.d("read", "read over");
                return 1;
            }
        }

        public int read() throws IOException
        {
            return 0;
        }

        public int read(byte[] b) throws IOException
        {
            return 0;
        };
    };
    

    public PlayThread(byte[] b)
    {
        playByte = b;
        final String t = "#!AMR\n";
        System.arraycopy(t.getBytes(), 0, playByte, 0, t.length());
    }


    /**
     * play the pcm stream
     */
    public void run()
    {
        /*
         * stream = new ByteArrayInputStream(playByte);
         * //ByteArrayInputStreamDataSource source = new
         * ByteArrayInputStreamDataSource(stream, "audio/x-pcm"); try { player =
         * Manager.createPlayer(stream, "audio/x-pcm"); player.start(); } catch
         * (IOException e) { Log.d("read","IOException"); e.printStackTrace(); }
         * catch (MediaException e) { Log.d("read","MediaException");
         * e.printStackTrace(); }
         */

        // ================Stream test====================
        
        try
        {
            mInput = new MyDataSource(playByte);
            player = Manager.createPlayer(mInput, "audio/x-pcm");
            player.start();
        }
        catch (IOException e)
        {
            Log.d("read", "IOException:" + e.toString());
            e.printStackTrace();
        }
        catch (MediaException e)
        {
            Log.d("read", "MediaException:" + e.toString());
            e.printStackTrace();
        }
         

        /*
        try
        {
            player = Manager.createPlayer(new DataSource(null)
            {
                SourceStream[] mStream = {mInput};

                public void connect() throws IOException
                {
                    Log.d("read", "createPlayer: connect data source");
                    // ssLogger.info("connect data source");
                }

                public void disconnect()
                {
                    Log.d("read", "createPlayer: disconnect data source");
                    // sLogger.info("disconnect data source");
                }

                public String getContentType()
                {
                    Log.d("read", "createPlayer: getContentType");
                    return contentType;
                    // return "audio/basic;codec=pcm";
                }

                public SourceStream[] getStreams()
                {
                    Log.d("read", "createPlayer: getStreams");
                    return mStream;
                }

                public void start() throws IOException
                {
                    Log.d("read", "createPlayer: start data source");
                    // sLogger.info("start data source");
                }

                public void stop() throws IOException
                {
                    Log.d("read", "createPlayer: stop data source");
                    // sLogger.info("start data source");
                }

                public Control getControl(String controlType)
                {
                    Log.d("read", "createPlayer: getControl");
                    return null;
                }

                public Control[] getControls()
                {
                    Log.d("read", "createPlayer: getControls");
                    return null;
                }
            });
            player.addPlayerListener(this);
            player.realize();
            AudioPathControl lPathCtr = (AudioPathControl) player
                .getControl("net.rim.device.api.media.control.AudioPathControl");
            lPathCtr.setAudioPath(AudioPathControl.AUDIO_PATH_HANDSET);
            player.prefetch();
            player.start();
        }
        catch (MediaException e)
        {
            Log.d("read", "MediaException" + e.toString());
            e.printStackTrace();
        }
        catch (IOException e)
        {
            Log.d("read", "IOException" + e.toString());
            e.printStackTrace();
        }
        Log.d("read", "player ready");
        */
    }

    /**
     * stop play
     */
    public void stop()
    {
        if (player != null)
        {
            try
            {
                if (player.getState() == Player.STARTED)
                {
                    player.stop();
                }
                if (player.getState() != Player.CLOSED)
                {
                    player.close();
                }
            }
            catch (MediaException e)
            {
                Log.d("read", "Error stopping reveive stream" + e);
                e.printStackTrace();
            }
        }
    }

    public void playerUpdate(Player player, String event, Object eventData)
    {
        // TODO Auto-generated method stub

    }
}
